// pages/myself/introduce/introduce.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: {},
    src: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.api.get({
      url: 'cpic_user_getUserInfoForVX'
    }).then(res => {
      this.setData({ info: res })
      this.setData({
        info: res,
        src: app.globalData.userInfo.imgResUrl
      })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  editInfo: function (e) {
    const { key } = e.currentTarget.dataset
    const { id } = this.data.info
    // console.log(key === 'workNum', id, this.data.info[key], this.data.info)
    if (key === 'workNum') {
      wx.showToast({
        title: '工号不可编辑',
        icon: 'none',
        duration: 2000
      })
    } else {
      wx.navigateTo({
        url: `/myself/pages/edit-info/editInfo?id=${id}&key=${key}&value=${this.data.info[key]}`,
      })
    }
  },
  watchAvatar: function (e) {
    console.log(e)
    wx.navigateTo({
      url: `/pages/view-avatar/viewAvatar?src=${this.data.src}`,
    })
  },
  updataImage () {
    app.api.chooseImageUpload(1, false).then(res => {
      this.data.src = res.data.url
      app.globalData.userInfo.imgResUrl =  res.data.url
      wx.setStorageSync("userInfo", JSON.stringify(app.globalData.userInfo))
      this.setData({
        src: this.data.src
      })
    })
  },
  editAwards: function (e) {
    const { key } = e.currentTarget.dataset
    const { id } = this.data.info
    console.log(key, id, this.data.info[key], this.data.info)
    const value = encodeURIComponent(JSON.stringify(this.data.info[key]))
    wx.navigateTo({
      url: `/myself/pages/edit-awards/editAwards?id=${id}&type=${key}&value=${value}`,
    })
  }
})